package com.earthchen.demo.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.earthchen.core.support.shiro.domain.Account;
import com.earthchen.core.support.shiro.service.AccountService;
import com.earthchen.demo.domain.SysUser;
import com.earthchen.demo.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author earthchen
 * @date 2018/9/4
 **/
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private SysUserService userService;

    @Override
    public Account loadAccount(String appId) {
        EntityWrapper<SysUser> sysUserEntityWrapper = new EntityWrapper<>();
        sysUserEntityWrapper.where("id", appId)
                .orNew("phone={0}", appId)
                .orNew("email={0}", appId);
        SysUser sysUser = userService.selectOne(sysUserEntityWrapper);
        return sysUser != null ? new Account(sysUser.getUsername(), sysUser.getPassword(), sysUser.getSalt()) : null;
    }

    @Override
    public boolean isAccountExistByUid(Long uid) {
        SysUser user = userService.selectById(uid);
        return user != null ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public String loadAccountRole(String appId) {
        return userService.selectUserRoles(appId);
    }
}
